/*
 * $Id: GridToggleAction.java 24 2009-03-31 23:40:10Z aaron.day $
 */
package org.crazydays.gameplan.map.swing.event;


import java.awt.event.ActionEvent;

import javax.swing.Icon;
import javax.swing.JInternalFrame;
import javax.swing.event.InternalFrameEvent;

import org.crazydays.gameplan.GamePlan;
import org.crazydays.gameplan.map.swing.JMapFrame;
import org.crazydays.gameplan.swing.AbstractActionInternalFrameListener;

/**
 * GridToggleAction
 */
public class GridToggleAction
        extends AbstractActionInternalFrameListener
{
	/** serial version uid */
	public final static long serialVersionUID =
	        "$Id: GridToggleAction.java 24 2009-03-31 23:40:10Z aaron.day $"
	                .hashCode();

	/** icon: tick */
	protected static Icon tick = loadIcon("/com/famfamfam/silk/tick.png");

	/** icon: cross */
	protected static Icon cross = loadIcon("/com/famfamfam/silk/cross.png");

	/** game plan */
	protected GamePlan gamePlan;

	/**
	 * GridToggleAction constructor.
	 * 
	 * @param gamePlan GamePlan
	 */
	public GridToggleAction(GamePlan gamePlan)
	{
		super("Toggle Grid");

		setGamePlan(gamePlan);
	}

	/**
	 * Set game plan.
	 * 
	 * @param gamePlan GamePlan
	 */
	public void setGamePlan(GamePlan gamePlan)
	{
		this.gamePlan = gamePlan;
	}

	/**
	 * Get game plan.
	 * 
	 * @return GamePlan
	 */
	public GamePlan getGamePlan()
	{
		return gamePlan;
	}

	/**
	 * Call create map on the create map dialog.
	 * 
	 * @param e Event
	 * @see java.awt.event.ActionListener#actionPerformed(ActionEvent)
	 */
	public void actionPerformed(ActionEvent e)
	{
		LOG.warn(e);

		JInternalFrame frame = getGamePlan().getActiveInternalFrame();
		if (frame instanceof JMapFrame) {
			JMapFrame mapFrame = (JMapFrame) frame;

			if (mapFrame.isGridVisible()) {
				mapFrame.setGridVisible(false);
				putValue(SMALL_ICON, cross);
			} else {
				mapFrame.setGridVisible(true);
				putValue(SMALL_ICON, tick);
			}
		}
	}

	/**
	 * @param e Event
	 * @see AbstractActionInternalFrameListener#internalFrameActivated(InternalFrameEvent)
	 */
	public void internalFrameActivated(InternalFrameEvent e)
	{
		LOG.debug(e);

		JInternalFrame frame = e.getInternalFrame();
		if (frame instanceof JMapFrame) {
			JMapFrame mapFrame = (JMapFrame) frame;

			if (mapFrame.isGridVisible()) {
				putValue(SMALL_ICON, tick);
			} else {
				putValue(SMALL_ICON, cross);
			}
		}
	}
}
